<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <TreeTool :tree-node="company" :is-root="true" @addDepts="addDepts" />
        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
          <TreeTool slot-scope="{ data }" :tree-node="data" @addDepts="addDepts" @getDepartments="getDepartments" @editDept="editDept" />
        </el-tree>
      </el-card>
      <!-- 添加部门的弹框 -->
      <AddDept ref="deptForm" :is-add-dept.sync="isAddDept" :node="node" @addDept="getDepartments" />
    </div>
  </div>
</template>

<script>
import TreeTool from './components/tree-tools.vue'
import AddDept from './components/add-dept.vue'
import { inquireDepartment } from '@/api/departments.js' // 接口
import { tranListToTreeData } from '@/utils/index.js' // 树状图的规则
export default {
  components: {
    TreeTool, AddDept
  },
  data() {
    return {
      company: {}, // title
      departs: [], // 树状图数据
      // 树状图的遵循规则
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      isAddDept: false, // 弹框是否显示
      node: null // 某个部门
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    // 获取企业的部门列表-----------------
    async getDepartments() {
      const data = await inquireDepartment()
      // console.log(data)
      this.company = { name: data.companyName, manager: '负责人', pid: '' }
      this.departs = tranListToTreeData(data.depts, '')
    },
    // 点击添加子部门  父组件接收值
    addDepts(node) {
      // 让弹框显示
      this.isAddDept = true
      // 接收子组件传过来的 参数
      this.node = node
    },
    // 点击编辑子部门
    editDept(node) {
      // 让弹框显示
      this.isAddDept = true
      // 调用子组件的方法
      // console.log(node)
      this.$refs.deptForm.getDepartDetail(node)

      // 接收子组件传过来的 参数
      this.node = node
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}

</style>

